<?php

	# AccountController Class: Controller for user account functionality
	class AccountController extends CustomControllerAction {
		
		protected $_redirector = null;
		protected $_simple_message_namespace = null;
		protected $_flash = null;

		public function init() {
			parent::init();

			$this->_flash = $this->_helper->getHelper('FlashMessenger');
			$this->_redirector = $this->_helper->getHelper('Redirector');
			$this->_simple_message_namespace = new Zend_Session_Namespace('simple_messages');
		}
	
		# indexAction: Controls index page
		public function indexAction() {

			# set up left tabs	
			$left_tabs = array(
				'Media' => array('/media/' . $user),
				'Comments' => array('/comments/' . $user)
			);

			# set up right tabs
			$auth = Zend_Auth::getInstance();
			if ($auth->hasIdentity())
			{
				$selected = 'Home';
				$right_tabs = array(
					'Home' => array('/home'),
					'Control Panel' => array('/cpanel')
				);
			}
			else
			{
				$selected = 'Login';
				$right_tabs = array(
					'Login' => array('/login')
				);
			}
			
			$this->view->tabs = array('selected' => $selected, 'left_tabs' => $left_tabs, 'right_tabs' => $right_tabs);
		}

		# detailsAction: Controls associated with editing user information
		public function detailsAction() {
			$user_db_mgr = new UserSDBManager();
			$auth = Zend_Auth::getInstance();
			$request = new Zend_Controller_Request_Http();

			# Get user's username 
			$identity = $auth->getIdentity();
			$username = $identity->username;

			# get user
			$users = $user_db_mgr->getByFieldValue('username', $username);
			$user = $users[0];

			# Update details
			if ($request->getParam('submit')) {

				$attributes = array(
												"email" => $request->getParam('email'),
												"phone" => $request->getParam('phone'),
												"carrier" => $request->getParam('carrier'),
												"likes" => $request->getParam('likes'),
												"dislikes" => $request->getParam('dislikes')
											);

				# Verify password fields
				if ($request->getParam('password')) {
					if ($request->getParam('password') == $request->getParam('password2')) {
						$attributes['password'] = sha1($request->getParam('password'));
					}
					else {
						$this->_simple_message_namespace->error = "Please confirm passwords";
						$this->_redirector->gotoUrl('/account/details');
					}
				}

				# Update the record
				if (!isSet($this->_simple_message_namespace->error)) {
					$user->setAttributes($attributes);
					$user->save(); // try catch?
		
					$this->_simple_message_namespace->success = "Your details were updated";
					$this->_redirector->gotoUrl('/account/details');
				}
			}

			# Set a success message, if present
			if (isSet($this->_simple_message_namespace->success)) {
				$this->view->success = $this->_simple_message_namespace->success;
				unset($this->_simple_message_namespace->success);
			}

			# Set an error message, if present
			if (isSet($this->_simple_message_namespace->error)) {
				$this->view->error = $this->_simple_message_namespace->error;
				unset($this->_simple_message_namespace->error);
			}

			# Get user details from the database
			$this->view->details = $user->getAttributes();
		}
	}
